<template>

    <div class="content-content" >
      <div class="p-img" @click="handletodetail(ia.GoodsId)">
        <img  class="p-img-content" :src="ia.GoodsImg" alt="" />
      </div>
      <div class="p-dec">
        <p class="dec" @click="handletodetail(ia.GoodsId)">{{ia.GoodsName}}</p>
        <div class="goods">￥{{ia.GoodsPrice}}
          <div class="jj">
            <img @click="handleJJ(ia)" v-if="num>0" class="jian" src="../../../static/images/jian.png" alt="" />
            <p class="num" v-if="num>0">{{num}}</p>
            <img @click="handleJ(ia)" class="jia" src="../../../static/images/jia.png" alt="" />
          </div>
        </div>
      </div>
    </div>
</template>

<script>
  import Self from '@/self'
  export default{
    props: [
      'ia'
    ],
    data() {
      return {
            num:0,
            goods:{},
            ThegoodID:'',
      }
    },
    onLoad(){
      // let TgoodID=this.ia.GoodsId;
      // console.log(TgoodID);
      // if( wx.getStorageSync(TgoodID)){
      //   this.num = wx.getStorageSync(TgoodID);
      // }
    },
    onShow(){
      // this.num = wx.getStorageSync('key')
      // console.log(this.ia);
      let TgoodID=this.ia.GoodsId;
      // console.log(TgoodID);
      if( wx.getStorageSync(TgoodID)){
        this.num = wx.getStorageSync(TgoodID);
      }
     // console.log('jiajian.onshow:'+this.num)

    },
    methods:{
      handletodetail(x){
        wx.navigateTo({
          url: '/pages/goods/main?index=' + x + '&thenum='+ this.num
        })
      },
      handleJ(ia){

        this.num++;
        this.goods.cishu =this.num;
        this.goods.goodsname=ia.GoodsName;
        this.goods.price=ia.GoodsPrice;
        this.goods.goodsid=ia.GoodsId;
        this.goods.goodsimg=ia.GoodsImg;
        this.goods.choose=true;
        // console.log(ia);
        console.log(this.num);
        Self.$emit('add',this.goods);
        wx.setStorageSync(this.goods.goodsid,this.num.toString());
        // console.log(wx.getStorageSync('goodsid'));
        // console.log(this.ia.GoodsId)

      },
      handleJJ(ia){

        // console.log(ia);
        let that=this;

        wx.showModal({
          title: '大哥',
          content: '小弟我这就给您跪下了 别点确定阿',
          success (res) {
            if (res.confirm) {
              that.num--;
             let  b=ia.GoodsId;
             let a=ia;
              Self.$emit('remove',a);
              if(b){
                wx.setStorageSync(b,that.num.toString());
              }

            } else if (res.cancel) {
            }
          }
        })

      }
    },
    mounted() {
      //接收A组件的数据
      Self.$on("addonegoods", function (a) {

         if(a.goodsid===this.ia.GoodsId){
           this.num ++;//总次数++

         }

        // console.log(this.goodlist)
      }.bind(this));
      Self.$on("removed", function (b) {

        if(b.goodsid===this.ia.GoodsId){
          this.num --;
        }else if(b.goodsid) {
          let xx =  wx.setStorageSync(b.goodsid);
          xx--;
          wx.getStorageSync(b.goodsid,xx);

        }
        if(b==='over'){
          this.num=0;
        }
        // console.log(this.num)

      }.bind(this));



    },



  }

</script>
<style scoped>
  .content-content{
    height: 200rpx;
    background: #ffffff;
    display: flex;
    border-bottom:1px solid 	#fffff0;
  }
  .p-dec{
    height: 100%;
    width:75%;

  }
  .p-img{
    height: 100%;
    width:25%;
    text-align: center;
  }
  .p-img-content{
    height: 80%;
    width:80%;
    text-align: center;
    margin:0 auto;
  }
  .dec{
    height:60%;
    width:85%;
    font-size:32rpx;
    margin-top:4rpx;
    color:#262626;

  }
  .goods{
    height: 40%;
    width:100%;
    color:red;
    position: relative;
  }
  .jj{
    display: flex;

  }
  .jia{
    height:50rpx;
    width:50rpx;
    position:absolute;
    top:0rpx;
    right: 41rpx;

  }
  .jian{
    height:45rpx;
    width:45rpx;
    position:absolute;
    top:0rpx;
    right: 131rpx;

  }
  .num{
    height:50rpx;
    width:50rpx;
    position:absolute;
    top:0rpx;
    right: 67rpx;

  }
</style>
